package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"managementsystem/common"
	"managementsystem/pkg/utils"
)

func GinGetScore(ctx *gin.Context) {
	db := common.Connect()
	defer db.Close()

	var data utils.StudentScore

	//查询学号是否存在
	ctx.ShouldBindBodyWith(&data,binding.JSON)
	//data.Sno = ctx.PostForm("sno")
	fmt.Println(data.Sno)
	//数据验证
	if len(data.Sno) != 6 {
		ctx.JSON(200, gin.H{"code": 200, "msg": "学号必须6位"})
		return
	}
	if isSnoExists(db, data.Sno) == false {
		ctx.JSON(200, gin.H{
			"msg": "学号不存在, 查询失败",
		})
		return
	}

	//获取参数
	score := common.SelectScore(db,data.Sno)
	fmt.Println(score)
	ctx.ShouldBindBodyWith(&score,binding.JSON)
	//返回结果
	ctx.JSON(200, score)
}
